<?php
declare (strict_types = 1);

namespace app\index\middleware;

#[('middleware AuthSession', 'is user login')]
class AuthSession
{
    public function handle($request, \Closure $next)
    {
        $user_data = session(config('user.session'));

        switch ($request->pathinfo()) {
            case 'login':
            case 'login/index':
            case 'login/login':
                if ($user_data && $user_data['username']) {
                    return redirect('/index');
                }
                break;
            case 'reg':
            case 'logout':
            case 'login/reg':
                break;

            default:
                if (empty($user_data)) {
                    return redirect('/index/login/index');
                }
        }

        #[('前置中间件')]
        $response = $next($request);

        #[('后置中间件')]
        return $response;
    }

    #[('中间件结束调度')]
    public function end(\think\Response $response)
    {}
}
